<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Ajax extends Controller {

	public function action_loadplaces() {
		if ($this->request->param('type') == 'city') {
			echo View::factory('pieces/places', array('catalog' => Model::factory('places')->get_cities($this->request->param('id')), 'currenttype' => 'city', 'placetype' => 'hotel', 'nextlevel' => TRUE))->render();
		} elseif ($this->request->param('type') == 'hotel') {
			echo View::factory('pieces/places', array('catalog' => Model::factory('places')->get_hotels($this->request->param('id')), 'currenttype' => 'hotel', 'placetype' => '', 'nextlevel' => FALSE))->render();
		}
	}

	public static function howMany($count, $one, $two, $many) {
		$tail = $count % 100;

		if ($tail > 20 || $tail < 5) {
			switch($tail % 10) {
				case 1:
					$many = $one;
					break;
				case 2:
				case 3:
				case 4:
					$many=$two;
			}
		}

		return str_replace('@value@', $count, $many);
	}

	public function action_from() {
		$count = Model::factory('country')->countCountries();
		$ostatok = $count[0]['count(country_id)'] % 30;
		$celoe = intval($count[0]['count(country_id)'] / 30);

		if ($ostatok > 0) {
			$pages = $celoe + 1;
		}
		else {
			$pages = $celoe;
		}

		$currentPage = $this->request->param('page');

		if ($currentPage == 1) {
			$offset = 0;
		}
		else {
			$offset = $currentPage*30-30;
		}

		$this->response->body(View::factory('ajax/countries', 
			array(
				'title' => 'Выберите страну',
				'countries' => Model::factory('country')->countries($offset),
				'pages' => $pages,
				'currentPage' => $this->request->param('page'),
				'action' => 'from'
			)
		));
	}

	public function action_where() {
		$count = Model::factory('country')->countCountries();
		$ostatok = $count[0]['count(country_id)'] % 30;
		$celoe = intval($count[0]['count(country_id)'] / 30);

		if ($ostatok > 0) {
			$pages = $celoe + 1;
		}
		else {
			$pages = $celoe;
		}

		$currentPage = $this->request->param('page');

		if ($currentPage == 1) {
			$offset = 0;
		}
		else {
			$offset = $currentPage*30-30;
		}

		$this->response->body(View::factory('ajax/countries', 
			array(
				'title' => 'Выберите страну',
				'countries' => Model::factory('country')->countries($offset),
				'pages' => $pages,
				'currentPage' => $this->request->param('page'),
				'action' => 'where'
			)
		));
	}

	public function action_city() {
		$count = Model::factory('country')->countCities($this->request->param('country_id'));
		$ostatok = $count[0]['count(city_id)'] % 30;
		$celoe = intval($count[0]['count(city_id)'] / 30);

		if ($ostatok > 0) {
			$pages = $celoe + 1;
		}
		else {
			$pages = $celoe;
		}

		$currentPage = $this->request->param('page');

		if ($currentPage == 1) {
			$offset = 0;
		}
		else {
			$offset = $currentPage*30-30;
		}

		$this->response->body(View::factory('ajax/cities', 
			array(
				'title' => 'Выберите город',
				'cities' => Model::factory('country')->cities($this->request->param('country_id'), $offset),
				'pages' => $pages,
				'currentPage' => $this->request->param('page')
			)
		));
	}

	public function action_aviacompany() {
		$city_from = explode('!', $_POST['fromCity']);
		$city_to = explode('!', $_POST['where']);

		if(empty($city_from[1])){
			echo '<nobr>Не выбран пункт отправки</nobr>';
		}elseif(empty($city_to[1])){
			echo '<nobr>Не выбран пункт назначения</nobr>';
		}else{
			$this->response->body(View::factory('ajax/aviacompany', 
				array(
					'title' => 'Выберите авиакомпанию',
					'aviacompany' => Model::factory('aviacompany')->getAll($city_from[1], $city_to[1])
				)
			));
		}

	}

	public function action_when() {
		$this->response->body(View::factory('ajax/when', 
			array(
				'title' => 'Выберите дату',
			)
		));
	}

	public function action_nights() {
		$this->response->body(View::factory('ajax/nights', 
			array(
				'title' => 'На сколько ночей',
			)
		));
	}

	public function action_package() {
		$city_to = explode('!', $_POST['where']);
		if(empty($city_to[1])){
			echo '<nobr>Не выбран пункт назначения</nobr>';
		}else{
			$this->response->body(View::factory('ajax/package', 
				array(
					'title' => 'Выберите тур-пакет',
					'packages' => Model::factory('package')->getItems($city_to[1])
				)
			));
		}
	}

	public function action_hotel() {
		$city_to = explode('!', $_POST['where']);
		$package = explode('!', $_POST['package']);

		if(empty($city_to[1])){
			echo '<nobr>Не выбран пункт назначения</nobr>';
		}else{
			$this->response->body(View::factory('ajax/hotels', 
				array(
					'title' => 'Выберите отель',
					'hotels' => Model::factory('hotels')->getItems($city_to[1], $package[1])
				)
			));
		}
	}

	public function action_roomType() {
		$this->response->body(View::factory('ajax/roomType', 
			array(
				'title' => 'Тип номера отеля',
			)
		));
	}

	public function action_adults() {
		$this->response->body(View::factory('ajax/adults', 
			array(
				'title' => 'Количество взрослых',
			)
		));
	}

	public function action_childrens() {
		$this->response->body(View::factory('ajax/childrens', 
			array(
				'title' => 'Количество детей',
			)
		));
	}

	public function action_additional() {
		$this->response->body(View::factory('ajax/additional', 
			array(
				'title' => 'Дополнительные услуги',
			)
		));
	}

	public function action_peoples() {
		$this->response->body(View::factory('ajax/peoples', 
			array(
				'title' => 'Количество человек',
			)
		));
	}

	public function action_pitanie() {
		$this->response->body(View::factory('ajax/pitanie', 
			array(
				'title' => 'Питание',
			)
		));
	}

}